// pages/goods/details.js
const app = getApp();
var com = require("../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodList : {},
    goods : false,
    checkAll : false,
    totalCount: '0',
    totalPrice: '0.00'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {

  },
  /**
   * 显示页面，执行一次
   */
  onShow: function () {
    if (app.globalData.userInfo) {
      com.post('Api/Home/car_list', { uid: app.globalData.userInfo.uid}, "setContent", this);
    } else {
      wx.switchTab({
        url: '../my/index'
      })
    }
  },
  setContent : function(e){
    var car_list = e.list;
    if (!car_list){
      return {};
    }
    for (var j = 0; j < car_list.length; ++j) {
      car_list[j].checked = false;
    }
    this.setData({
      goodList: car_list,
      goods : true
    });
  },
  /**
   * 用户选择购物车商品
   */
  checkboxChange: function (e) {
    var checkboxItems = this.data.goodList;
    var values = e.detail.value;
    for (var i = 0; i < checkboxItems.length; ++i) {
      checkboxItems[i].checked = false;
      for (var j = 0; j < values.length; ++j) {
        if (checkboxItems[i].id == values[j]) {
          checkboxItems[i].checked = true;
          break;
        }
      }
    }
    var checkAll = false;
    if (checkboxItems.length == values.length) {
      checkAll = true;
    }
    this.setData({
      'goodList': checkboxItems,
      'checkAll': checkAll
    });
    this.calculateTotal();
  },
  /**
 * 计算商品总数
 */
  calculateTotal: function () {
    var goodList = this.data.goodList;
    var totalCount = 0;
    var totalPrice = 0;
    for (var i = 0; i < goodList.length; i++) {
      var good = goodList[i];
      if (good.checked) {
        totalCount += good.count;
        totalPrice += good.count * good.shop.price;
      }
    }
    totalPrice = totalPrice.toFixed(2);
    this.setData({
      'totalCount': totalCount,
      'totalPrice': totalPrice
    })
  },
  /**
  * 用户点击商品减1
  */
  subtracttap: function (e) {
    var index = e.target.dataset.index;
    var goodList = this.data.goodList;
    var count = goodList[index].count;
    if (count <= 1) {
      return;
    } else {
      goodList[index].count--;
      this.setData({
        'goodList': goodList
      });
      this.calculateTotal();
    }
    com.post('Api/Home/car_count', { id: goodList[index].id, count: goodList[index].count }, "setCount", this);
  },
  /**
   * 用户点击商品加1
   */
  addtap: function (e) {
    var index = e.target.dataset.index;
    var goodList = this.data.goodList;
    var count = goodList[index].count;
    goodList[index].count++;
    this.setData({
      'goodList': goodList
    });
    this.calculateTotal();
    com.post('Api/Home/car_count', { id : goodList[index].id,count: goodList[index].count }, "setCount", this);
  },  
  /**
   * 用户点击全选
   */
  selectalltap: function (e) {
    var value = e.detail.value;
    var checkAll = false;
    if (value && value[0]) {
      checkAll = true;
    }
    var goodList = this.data.goodList;
    for (var i = 0; i < goodList.length; i++) {
      goodList[i]['checked'] = checkAll;
    }
    this.setData({
      'checkAll': checkAll,
      'goodList': goodList
    });
    this.calculateTotal();
  },
  /**
   * 更新商品数量
   */
  setCount: function () {

  },
  /**
   * 下单页面
   */
  goOrder: function () {
    var shop_id = '';
    var goodList = this.data.goodList;
    for (var i = 0; i < goodList.length; i++) {
      if (goodList[i].checked == true){
        shop_id = shop_id + ',' + goodList[i].id;
      }
    }
    if (!shop_id){
      return {};
    }
    wx.navigateTo({
      url: "order?id=" + shop_id,
    })
  },
  /**
   * 跳转到商品分类
   */
  goShop : function(){
    wx.switchTab({
      url: '../goods/index'
    })
  }
})